﻿/*
Copyright (c) 2013, Marc Tanti
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the www.marctanti.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MARIA.level.square;
using System.Drawing;
using MARIA.level.robot;
using MARIA.level.situation.ended_reason;

namespace MARIA.level.situation
{
    /// <summary>
    /// A situation which the robot needs to get through.
    /// </summary>
    public class Situation
    {

        /// <summary>
        /// The name of the situation.
        /// </summary>
        public string Name
        {
            get
            {
                return name;
            }
        }
        private string name;

        /// <summary>
        /// The robot which is used in this situation.
        /// </summary>
        public Robot ResidingRobot
        {
            get
            {
                return robot;
            }
        }
        private Robot robot;

        /// <summary>
        /// The map of the squares in the situation.
        /// </summary>
        public Square[,] Map
        {
            get
            {
                return map;
            }
        }
        private Square[,] map;

        /// <summary>
        /// The number of movements left for the robot.
        /// </summary>
        public int MovementsLeft
        {
            get
            {
                return movementsLeft;
            }
        }
        private int movementsLeft;

        /// <summary>
        /// The number of movements allowed for the robot.
        /// </summary>
        private int numMovementsAllowed;

        /// <summary>
        /// Whether the situation has ended.
        /// </summary>
        public bool HasEnded
        {
            get
            {
                return hasLost || hasWon;
            }
        }

        /// <summary>
        /// Whether the situation was lost.
        /// </summary>
        public bool HasLost
        {
            get
            {
                return hasLost;
            }
        }
        private bool hasLost;
        
        /// <summary>
        /// Whether the situation was won.
        /// </summary>
        public bool HasWon
        {
            get
            {
                return hasWon;
            }
        }
        private bool hasWon;

        /// <summary>
        /// The reason the situation ended, or null if the situation hasn't ended.
        /// </summary>
        public SituationEndedReason ReasonEnded
        {
            get
            {
                return reasonEnded;
            }
        }
        private SituationEndedReason reasonEnded;

        /// <summary>
        /// Observer for when the situation ends.
        /// </summary>
        /// <param name="sender">The situation object which fires the event.</param>
        /// <param name="args">The event arguments.</param>
        public delegate void SituationEndedHandler(object sender, SituationEndedEventArgs args);
        
        /// <summary>
        /// An event which fires when the situation ends.
        /// </summary>
        public event SituationEndedHandler OnSituationEnded;

        /// <summary>
        /// Initialize a new instance of Robot using a map of the situation and the initial robot location.
        /// </summary>
        /// <param name="name">The name of the situation.</param>
        /// <param name="map">The map of the situation.</param>
        /// <param name="robotLocation">The initial location of the robot.</param>
        public Situation(string name, Square[,] map, Point robotLocation, int numMovementsAllowed)
        {
            this.name = name;
            this.hasLost = false;
            this.hasWon = false;
            this.map = map;
            this.reasonEnded = null;
            this.robot = new Robot(this, robotLocation);
            this.movementsLeft = numMovementsAllowed;
            this.numMovementsAllowed = numMovementsAllowed;
            
            this.robot.OnRobotMoved += new Robot.RobotMovedHandler(robot_OnRobotMoved);
        }

        /// <summary>
        /// Reinitialize the situation.
        /// </summary>
        public void Reinitialize()
        {
            this.hasLost = false;
            this.hasWon = false;
            this.reasonEnded = null;
            this.robot.Reinitialize();
            this.movementsLeft = numMovementsAllowed;
        }

        /// <summary>
        /// Observer to when the robot moves.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="args">Arguments of the event.</param>
        private void robot_OnRobotMoved(object sender, RobotMovedEventArgs args)
        {
            if (HasEnded)
            {
                throw new AttemptingToUseTerminatedSituationException();
            }

            if (movementsLeft < 0)
            {
                Lost(SituationLostDueToTooManyMovements.Instance);
            }
            else
            {
                movementsLeft--;
                GetSquare(args.NewLocation).SteppedOnResult(this);
            }
        }

        /// <summary>
        /// Get a square off the map.
        /// </summary>
        /// <param name="location">The location of the queried square.</param>
        /// <returns>The square.</returns>
        public Square GetSquare(Point location)
        {
            if (HasEnded)
            {
                throw new AttemptingToUseTerminatedSituationException();
            }

            if (location.Y < 0 || location.Y >= map.GetLength(0) || location.X < 0 || location.X >= map.GetLength(1))
            {
                return OutsideMapSquare.Instance;
            }
            else
            {
                return map[location.Y, location.X];
            }
        }

        /// <summary>
        /// Make the situation lost.
        /// </summary>
        /// <param name="reason">The reason the situation was lost.</param>
        public void Lost(SituationLostReason reason)
        {
            if (HasEnded)
            {
                throw new AttemptingToUseTerminatedSituationException();
            }

            hasLost = true;
            reasonEnded = reason;

            OnSituationEnded(this, new SituationEndedEventArgs(this));
        }

        /// <summary>
        /// Make the situation won.
        /// </summary>
        /// <param name="reason">The reason the situation was won.</param>
        public void Win(SituationWonReason reason)
        {
            if (HasEnded)
            {
                throw new AttemptingToUseTerminatedSituationException();
            }

            hasWon = true;
            reasonEnded = reason;

            OnSituationEnded(this, new SituationEndedEventArgs(this));
        }

    }
}
